<?php

class Captcha {

    private $width;
    private $height;
    private $codeNum;
    private $code;
    private $im;

    function __construct($width = 60, $height = 20, $codeNum = 4) {
        $this->width = $width;
        $this->height = $height;
        $this->codeNum = $codeNum;
    }

    function showImg() {
        //创建图片
        $this->createImg();
        //设置干扰元素
        $this->setDisturb();
        //设置验证码
        $this->setCaptcha();
        //输出图片
        $this->outputImg();
    }

    function getCaptcha() {
        return $this->code;
    }

    private function createImg() {
        $this->im = imagecreatetruecolor($this->width, $this->height);
        $bgColor = imagecolorallocate($this->im, 255, 255, 255);
        imagefill($this->im, 0, 0, $bgColor);
    }

    private function setDisturb() {
        $area = ($this->width * $this->height) / 20;
        $disturbNum = ($area > 250) ? 250 : $area;
        //背景横线
        $lineColor1 = imagecolorallocate($this->im, 0xda, 0xd9, 0xd1);
        for ($j = 3; $j <= $img_height - 3; $j = $j + 3) {
            imageline($this->im, 2, $j, $img_width - 2, $j, $lineColor1);
        }

        //背景竖线
//        $lineColor2 = imagecolorallocate($this->im, 0xda, 0xd9, 0xd1);
//        for ($j = 2; $j < 100; $j = $j + 6) {
//            imageline($this->im, $j, 0, $j + 8, $img_height, $lineColor2);
//        }
    }

    private function createCode() {
        $str = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKMNPQRSTUVWXYZ";

        for ($i = 0; $i < $this->codeNum; $i++) {
            $this->code .= $str{rand(0, strlen($str) - 1)};
        }
    }

    private function setCaptcha() {
        $this->createCode();

        for ($i = 0; $i < $this->codeNum; $i++) {
            $size = rand(floor($this->height / 5), floor($this->height / 3));
            $x = floor($this->width / $this->codeNum) * $i + 5;
            $y = rand(0, $this->height - 20);
            //文字随机颜色
            $fontColor[] = imagecolorallocate($this->im, 0x15, 0x15, 0x15);
            $fontColor[] = imagecolorallocate($this->im, 0x95, 0x1e, 0x04);
            $fontColor[] = imagecolorallocate($this->im, 0x93, 0x14, 0xa9);
            $fontColor[] = imagecolorallocate($this->im, 0x12, 0x81, 0x0a);
            $fontColor[] = imagecolorallocate($this->im, 0x06, 0x3a, 0xd5);
            $c_fontColor = $fontColor[rand(0, 4)];
            imagechar($this->im, 6, $x, $y, $this->code{$i}, $c_fontColor);
        }
    }

    private function outputImg() {
        if (imagetypes() & IMG_JPG) {
            ob_clean();
            header('Content-type:image/jpeg');
            imagejpeg($this->im);
        } elseif (imagetypes() & IMG_GIF) {
            ob_clean();
            header('Content-type: image/gif');
            imagegif($this->im);
        } elseif (imagetype() & IMG_PNG) {
            ob_clean();
            header('Content-type: image/png');
            imagepng($this->im);
        } else {
            die("Don't support image type!");
        }
    }

}

?>